﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.Notification;
using System.Text.RegularExpressions;
using System.Web.Security;
using DNA.Mvc.Text;

namespace DNA.Mvc.Publishing.Observers
{
    public class ArticleUpdateObserver : TemplatedMailEventObserver<ArticleUpdateEvent>
    {
        private readonly Regex AuthorRegex = new Regex("\\{author\\}", RegexOptions.Compiled);
        private readonly Regex CatRegex = new Regex("\\{category\\}", RegexOptions.Compiled);
        private readonly Regex PostedRegex = new Regex("\\{posted\\}", RegexOptions.Compiled);
        private readonly Regex TitleRegex = new Regex("\\{title\\}", RegexOptions.Compiled);
        private readonly Regex SummaryRegex = new Regex("\\{summary\\}", RegexOptions.Compiled);
        private readonly Regex PermalinkRegex = new Regex("\\{permalink\\}", RegexOptions.Compiled);

        protected override string GetDestinationAddress(ArticleUpdateEvent e)
        {
            var user = Membership.GetUser(e.Article.UserName);
            if (user != null)
                return user.Email;
            return "";
        }

        string body = "";

        public override void Process(ArticleUpdateEvent e)
        {
            string content = ReadTemplateContent(e); ;

            if (!string.IsNullOrEmpty(Templated))
                OnFormatHtmlBody(e, ref content);
            else
                OnFormatTextBody(e, ref content);

            var wrapper = new CategoryWrapper(e.Article.Category);

            Notifier.Send(wrapper.PermaLink.ToLower(), wrapper.Title, body);
        }

        protected override void OnFormatHtmlBody(ArticleUpdateEvent e, ref string html)
        {
            var user = Membership.GetUser(e.Article.UserName);
            html = AuthorRegex.Replace(html, user.GetDisplayName());
            html = CatRegex.Replace(html, e.Article.Category.Title);
            html = TitleRegex.Replace(html, e.Article.Title);
            html = PostedRegex.Replace(html, e.Article.Posted.ToString());
            var artileWrapper = new ArticleWrapper(e.Article);
            string summary = GetSummary(e);
            html = SummaryRegex.Replace(html, summary);
            var permalink = artileWrapper.PermaLink;
            html = PermalinkRegex.Replace(html, permalink);
            body = html;
        }

        private static string GetSummary(ArticleUpdateEvent e)
        {
            string summary = e.Article.Summary;
            if (string.IsNullOrEmpty(summary))
            {
                var htmlBody = e.Article.Body;
                if ((TextFormats)e.Article.ContentFormat == TextFormats.WiKi)
                    htmlBody = TextEngine.Wiki(e.Article.Body.Replace("{toc}", "")).ToString();
                htmlBody = TextEngine.Text(htmlBody);

                if (htmlBody.Length < 255)
                    summary = htmlBody;
                else
                    summary = htmlBody.Substring(0, 255);
            }
            return summary;
        }

        protected override void OnFormatTextBody(ArticleUpdateEvent e, ref string text)
        {
            var user = Membership.GetUser(e.Article.UserName);
            var artileWrapper = new ArticleWrapper(e.Article);

            var sb = new StringBuilder();
            sb.AppendFormat("{0} has posted a new article to {1} - {2}", user.GetDisplayName(), e.Article.Category.Title, e.Article.Posted.ToString())
                .AppendLine()
                .AppendLine(e.Article.Title)
                .AppendLine(GetSummary(e))
                .AppendLine()
                .AppendFormat("Click the following link to read complete article : {0}", artileWrapper.PermaLink)
                .AppendLine()
                .AppendLine("Thanks");

            text = sb.ToString();
        }
    }
}
